<?php
class Action{
	
	public static $msgs = array();
		
	function __construct()
	{
	
	}
	
	
	function updateUser($data)
	{
		global $db;
		$user_id = (int) $data['user_id'];
		if( $user_id > 0 )
		{
			$record = array(
									'name' => $data['name'],
									'email'=> $data['email'],
									'phone_no'=> $data['phone_no'],
								);
			$where = array('user_id' => $user_id);
			$db->update("trivia_users", $record, $where);
			echo $user_id; exit;
		}
		echo 0;
	}
	
	 public function addUser($data)
    {
		global $db, $msgError;
		$data['imei_no'] = trim($data['imei_no']);
		if($data['imei_no'] != '')
		{
			$user_data = $db->first("SELECT * FROM trivia_users  WHERE imei_no = '".$db->escape($data['imei_no'])."'");
			if( intval($user_data['user_id']) > 0 ) // user already exist
			{
				$username = $user_data['username'];
			}
			else	// add new user
			{
				 $record = array('imei_no' => $data['imei_no']);
				 $user_id =  $db->insert("trivia_users", $record); 		
				 $ran_str = $this->get_rand_id(6);
				 $username = strtoupper($ran_str)."-".$user_id;
				 
				 $record = array('username' => $username);
				 $where = array('user_id' => $user_id);
				 $db->update("trivia_users", $record, $where);
			}	
			echo $username; exit;
		}
			echo 0; exit;
	}
	
	function getCategoriesXML()
	{
		global $db, $config;
		$sql = "SELECT * FROM category order by category_name ";
		$result = $db->fetch_all($sql);	
		$xml = "<categories>";
		foreach($result as $row)
		{
			$xml .= "<category>
							<category_id>".$row['category_id']."</category_id>
							<category_name>".trim($row['category_name'])."</category_name>
						</category>	";	
		}
		$xml .= "</categories>";
		
		header ("content-type: text/xml");
		echo $xml;
		exit;
		
	}
	
	function getQuestions($data)
	{
		global $db;
		$category_id = $data['category_id'];
		$user_id = $data['user_id'];
		$limit = $data['no_of_question'];
		
		if($limit == '' ){
			$limit = " limit 0, 3 ";
		} else {
			$limit = " limit 0, ".intval($limit)." ";	
		}
		
		$sql = "SELECT 
						DISTINCT q. * FROM quiz_question q 
				WHERE q.active = 1 AND q.category_id = '".$category_id."' AND q.question_id NOT IN ( SELECT a.question_id	FROM quiz_answer a WHERE a.user_id ='".$user_id."' ) $limit ";
				
		$questions = $db->fetch_all($sql);
		
		$xml = "<trivia>";
		
		foreach($questions as $row)
		{
			$xml .= "<question>
						<question_id>".$row['question_id']."</question_id>
						<question_stmt>".$row['question']."</question_stmt>
					   <answer_id>".$row['answer_id']."</answer_id>
						 <option>";
			
			$options = $this->getQuestionOptions($row['question_id']);
			foreach($options as $opt)
			{
				$xml .= "<option_id>".$opt['quiz_option_id']."</option_id>
				        <option_stmt>".$opt['quiz_option']."</option_stmt>";
			}
			
			$xml .= "</option></question>";
			
		}
		$xml .= "</trivia>";
		
		header ("content-type: text/xml");
		echo $xml;
		exit;
	}
	
	function getQuestionOptions($question_id)
	{
		global $db;
		$sql = "SELECT * FROM quiz_option WHERE question_id = '".$question_id."'";
		return $db->fetch_all($sql);		
	}
	
	function submitTrivia($data)
	{
		global $db;
		$success = 0;
		$user_id = (int)$data['user_id'];
		if( $user_id > 0 )
		{
			$questions = explode(',', $data['questions']);
			$answers = explode(',', $data['answers']);
			$created_on = date('Y-m-d H:i:s');
			$set_id = $this->getTrivaSetId();
			
			for($i = 0; $i < count($questions); $i++ )
			{
				if( intval($questions[$i]) > 0 )
				{
						$record = array(
						  'user_id' => $user_id,
						  'question_id' => $questions[$i],
						  'correct' => $answers[$i],
						  'set_id' => $set_id,
						  'created_on' => $created_on
						);
					
					$db->insert("quiz_answer", $record);	
					$success = 1;
				}
			}	
			
		}
		echo $success;
	}
	
	function getTrivaSetId()
	{
		global $db;
		$sql = "SELECT max(set_id) as max_id FROM quiz_answer limit 1";
		$data = $db->first($sql);
		$set_id = intval($data['max_id']) + 1;
		return $set_id;
	}
	
	function get_rand_id($length)
	{
		if($length>0)
		{
			$rand_id="";
			for($i=1; $i<=$length; $i++)
			{
				mt_srand((double)microtime() * 1000000);
				$num = mt_rand(1,26);
				$rand_id .= $this->assign_rand_value($num);
			}
		}
		return $rand_id;
	}
		
	function assign_rand_value($num)
	{
	
		switch($num)
		{
			case "1":
				$rand_value = "a";
				break;
			case "2":
				$rand_value = "b";
				break;
			case "3":
				$rand_value = "c";
				break;
			case "4":
				$rand_value = "d";
				break;
			case "5":
				$rand_value = "e";
				break;
			case "6":
				$rand_value = "f";
				break;
			case "7":
				$rand_value = "g";
				break;
			case "8":
				$rand_value = "h";
				break;
			case "9":
				$rand_value = "i";
				break;
			case "10":
				$rand_value = "j";
				break;
			case "11":
				$rand_value = "k";
				break;
			case "12":
				$rand_value = "l";
				break;
			case "13":
				$rand_value = "m";
				break;
			case "14":
				$rand_value = "n";
				break;
			case "15":
				$rand_value = "o";
				break;
			case "16":
				$rand_value = "p";
				break;
			case "17":
				$rand_value = "q";
				break;
			case "18":
				$rand_value = "r";
				break;
			case "19":
				$rand_value = "s";
				break;
			case "20":
				$rand_value = "t";
				break;
			case "21":
				$rand_value = "u";
				break;
			case "22":
				$rand_value = "v";
				break;
			case "23":
				$rand_value = "w";
				break;
			case "24":
				$rand_value = "x";
				break;
			case "25":
				$rand_value = "y";
				break;
			case "26":
				$rand_value = "z";
				break;
			case "27":
				$rand_value = "0";
				break;
			case "28":
				$rand_value = "1";
				break;
			case "29":
				$rand_value = "2";
				break;
			case "30":
				$rand_value = "3";
				break;
			case "31":
				$rand_value = "4";
				break;
			case "32":
				$rand_value = "5";
				break;
			case "33":
				$rand_value = "6";
				break;
			case "34":
				$rand_value = "7";
				break;
			case "35":
				$rand_value = "8";
				break;
			case "36":
				$rand_value = "9";
				break;
		}
		return $rand_value;
	}
	
  
}


?>